package com.yimicloud.common.svn;

import java.io.File;
import java.util.*;
import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNProperties;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.ISVNEditor;
import org.tmatesoft.svn.core.io.ISVNWorkspaceMediator;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNInfo;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNStatus;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

/**
 * Created by Justin on 2015/9/7.
 */
public class SvnUtil {
    public static final String adminUserName="admin";
    public static final String adminPassword="admin";

    public SvnUtil() {
    }

    public static void setupLibrary() {
        DAVRepositoryFactory.setup();
        SVNRepositoryFactoryImpl.setup();
        FSRepositoryFactory.setup();
    }

    public static SVNClientManager authSvn(String svnRoot, String username, String password) {
        setupLibrary();
        SVNRepository repository = null;

        try {
            repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(svnRoot));
        } catch (SVNException var7) {
            var7.printStackTrace();
            return null;
        }


        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray());
        repository.setAuthenticationManager(authManager);
        DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
        SVNClientManager clientManager = SVNClientManager.newInstance(options, authManager);
        return clientManager;
    }

    public static SVNCommitInfo makeDirectory(SVNClientManager clientManager, SVNURL url, String commitMessage) {
        try {
            return clientManager.getCommitClient().doMkDir(new SVNURL[]{url}, commitMessage);
        } catch (SVNException var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static SVNCommitInfo importDirectory(SVNClientManager clientManager, File localPath, SVNURL dstURL, String commitMessage, boolean isRecursive) {
        try {
            return clientManager.getCommitClient().doImport(localPath, dstURL, commitMessage, (SVNProperties)null, true, true, SVNDepth.fromRecurse(isRecursive));
        } catch (SVNException var6) {
            var6.printStackTrace();
            return null;
        }
    }

    public static void addEntry(SVNClientManager clientManager, File wcPath) {
        try {
            clientManager.getWCClient().doAdd(new File[]{wcPath}, true, false, false, SVNDepth.INFINITY, false, false, true);
        } catch (SVNException var3) {
            var3.printStackTrace();
        }

    }

    public static SVNStatus showStatus(SVNClientManager clientManager, File wcPath, boolean remote) {
        SVNStatus status = null;

        try {
            status = clientManager.getStatusClient().doStatus(wcPath, remote);
        } catch (SVNException var5) {
            var5.printStackTrace();
        }

        return status;
    }

    public static SVNCommitInfo commit(SVNClientManager clientManager, File wcPath, boolean keepLocks, String commitMessage) {
        try {
            return clientManager.getCommitClient().doCommit(new File[]{wcPath}, keepLocks, commitMessage, (SVNProperties)null, (String[])null, false, false, SVNDepth.INFINITY);
        } catch (SVNException var5) {
            var5.printStackTrace();
            return null;
        }
    }

    public static long update(SVNClientManager clientManager, File wcPath, SVNRevision updateToRevision, SVNDepth depth) {
        SVNUpdateClient updateClient = clientManager.getUpdateClient();
        updateClient.setIgnoreExternals(false);

        try {
            return updateClient.doUpdate(wcPath, updateToRevision, depth, false, false);
        } catch (SVNException var6) {
            var6.printStackTrace();
            return 0L;
        }
    }

    public static long checkout(SVNClientManager clientManager, SVNURL url, SVNRevision revision, File destPath, SVNDepth depth) {
        SVNUpdateClient updateClient = clientManager.getUpdateClient();
        updateClient.setIgnoreExternals(false);

        try {
            return updateClient.doCheckout(url, destPath, revision, revision, depth, false);
        } catch (SVNException var7) {
            var7.printStackTrace();
            return 0L;
        }
    }

    public static boolean isWorkingCopy(File path) {
        if(!path.exists()) {
            System.out.println(path + "\' not exist!");
            return false;
        } else {
            try {
                if(SVNWCUtil.getWorkingCopyRoot(path, false) == null) {
                    return false;
                }
            } catch (SVNException var2) {
                var2.printStackTrace();
            }

            return true;
        }
    }

    public static boolean isURLExist(String svnUrl) {
        try {
            setupLibrary();
            SVNRepository e = null;
            e = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(svnUrl));
            ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(adminUserName, adminUserName.toCharArray());
            e.setAuthenticationManager(authManager);
            SVNNodeKind nodeKind = e.checkPath("", -1L);
            return nodeKind != SVNNodeKind.NONE;
        } catch (SVNException var4) {
            return false;
        }
    }

    public static SVNCommitInfo deleteDir(ISVNEditor editor, String dirPath) throws SVNException {
        editor.openRoot(-1L);
        editor.deleteEntry(dirPath, -1L);
        editor.closeDir();
        return editor.closeEdit();
    }

    public static void deleteSvnCode(String svnUrl, String appName) throws SVNException {
        setupLibrary();
        SVNRepository repository = null;
        repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(svnUrl));
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(adminUserName, adminPassword.toCharArray());
        repository.setAuthenticationManager(authManager);
        ISVNEditor editor = repository.getCommitEditor("directory and file delete", (ISVNWorkspaceMediator)null);
        deleteDir(editor, appName);
    }

    public static void createTag(String svnUrlAhthz, String svnUrlCheckOut, String svnUrlImport, String userName, String passWord, String filePath, String tagName, long revisionNum) throws SVNException {
        SVNClientManager svnClientManager = authSvn(svnUrlAhthz, userName, passWord);
        SVNURL url = SVNURL.parseURIDecoded(svnUrlCheckOut);
        File file1 = new File(filePath + "/" + tagName);
        if(revisionNum == 0L) {
            checkout(svnClientManager, url, SVNRevision.HEAD, file1, SVNDepth.INFINITY);
        } else {
            checkout(svnClientManager, url, SVNRevision.create(revisionNum), file1, SVNDepth.INFINITY);
        }

        url = SVNURL.parseURIEncoded(svnUrlImport);
        File file = new File(filePath);
        importDirectory(svnClientManager, file, url, "create tag", true);
    }

    public static long getLogNum(String svnUrlAhthz, String svnUrlLogNum) {
        long result = 0L;
        SVNClientManager svnClientManager = authSvn(svnUrlAhthz, adminUserName, adminPassword);

        try {
            SVNURL url = SVNURL.parseURIDecoded(svnUrlLogNum);
            SVNWCClient e = svnClientManager.getWCClient();
            SVNInfo info = e.doInfo(url, SVNRevision.HEAD, SVNRevision.HEAD);
            result = info.getCommittedRevision().getNumber();
        } catch (SVNException var8) {
            var8.printStackTrace();
        }

        return result;
    }

    public static boolean checkListRevision(String url, long revision) throws SVNException {
        long startRevision = 0L;
        long endRevision = -1L;
        setupLibrary();
        SVNRepository repository = null;
        repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(adminUserName, adminPassword.toCharArray());
        repository.setAuthenticationManager(authManager);
        Collection logEntries = null;
        boolean b = false;
        logEntries = repository.log(new String[]{""}, (Collection)null, startRevision, endRevision, true, true);
        Iterator entries = logEntries.iterator();

        while(entries.hasNext()) {
            SVNLogEntry logEntry = (SVNLogEntry)entries.next();
            long lg = logEntry.getRevision();
            if(revision == lg) {
                b = true;
                break;
            }
        }

        return b;
    }

    public static List<Map<String, Object>> getListRevision(String url) throws SVNException {
        long startRevision = 0L;
        long endRevision = -1L;
        setupLibrary();
        SVNRepository repository = null;

        try {
            repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
        } catch (SVNException var13) {
            var13.printStackTrace();
        }

        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(adminUserName, adminUserName.toCharArray());
        repository.setAuthenticationManager(authManager);
        Collection logEntries = null;
        boolean b = false;
        logEntries = repository.log(new String[]{""}, (Collection)null, startRevision, endRevision, true, true);
        ArrayList list = new ArrayList();
        Iterator entries = logEntries.iterator();

        while(entries.hasNext()) {
            SVNLogEntry logEntry = (SVNLogEntry)entries.next();
            HashMap map = new HashMap();
            map.put("revision", Long.valueOf(logEntry.getRevision()));
            list.add(map);
        }

        return list;
    }

    public static void main(String[] args) throws SVNException {
      /*  String appName = "asdff";
        String tagName = appName + 3.0D;
        String filePath = "d:/xml/";
        String svnUrlAhthz = "https://10.79.108.36/svn/yqcylx/source_code/01data-commons";
        String svnUrlImport = svnUrlAhthz + "/tag";
        long lg = 11111L;
        SVNClientManager svnClientManager = authSvn(svnUrlAhthz, "gujunfeng", "123456");
        File file = new File(filePath);

        System.out.println(isURLExist(svnUrlAhthz));
        checkout(svnClientManager, SVNURL.parseURIDecoded(svnUrlAhthz), SVNRevision.HEAD, file, SVNDepth.INFINITY);
        System.out.println("1212");*/

        SVNClientManager svnClientManager = authSvn("svn://192.168.33.10/repos", "admin", "admin");
        String filePath = "d:/xml/mm";
        File file = new File(filePath);
        //checkout(svnClientManager, SVNURL.parseURIDecoded("svn://192.168.33.10/repos"), SVNRevision.HEAD, file, SVNDepth.INFINITY);

        //SvnUtil.addEntry(svnClientManager,file);
        //SvnUtil.commit(svnClientManager,file,false,"add mm folder");
        Object t=  SvnUtil.getLogNum("svn://192.168.33.10/repos", "svn://192.168.33.10/repos");
        System.out.println(t);


    }
}
